package com.ac.api.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.ac.api.group.dao.AcActionMapper;
import com.ac.api.group.entity.AcAction;
import com.ac.api.group.service.AcActionService;
import com.ac.api.group.vo.AcActionVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcActionServiceImpl extends ServiceImpl<AcActionMapper, AcAction> implements AcActionService {

    @Override
    public List<AcActionVo> findByExample(AcActionVo example) {
        return this.baseMapper.findByExample(buildWrapper(example));
    }

    @Override
    public Page<AcActionVo> findByPage(AcActionVo example, Page page) {
        /** 不进行 count sql 优化，解决 MP 无法自动优化 SQL 问题 */
        page.setOptimizeCountSql(false);
        return page.setRecords(baseMapper.findByExample(buildWrapper(example), page));
    }

    @Override
    public List<AcAction> findByModuleid(Integer moduleid) {
        QueryWrapper<AcAction> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AcAction::getModuleid, moduleid)
                .eq(AcAction::getState, "1")
                .orderByAsc(AcAction::getActionnumber);
        return this.list(wrapper);
    }

    @Override
    public AcAction findByAcAction(String acAction) {
        return baseMapper.findByAction(acAction);
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<AcActionVo> buildWrapper(AcActionVo example) {
        QueryWrapper<AcActionVo> wrapper = new QueryWrapper<>();

        /** 条件查询 */
        String  name       = example.getName();
        String  action     = example.getAction();
        Integer moduleid   = example.getModuleid();
        String  moduleName = example.getModuleName();
        wrapper.like(StringUtils.isNotEmpty(moduleName), "module_name", moduleName);
        wrapper.lambda()
                .like(StringUtils.isNotEmpty(name), AcActionVo::getName, name)
                .like(StringUtils.isNotEmpty(action), AcActionVo::getAction, action)
                .eq(null != moduleid, AcActionVo::getModuleid, moduleid)
                .eq(AcActionVo::getState, "1")
                .orderByAsc(AcActionVo::getId);
        return wrapper;
    }
}
